﻿using System;
using BusinessBingo.Common.Interfaces;
using log4net;
using log4net.Config;

namespace BusinessBingo.Common
{
    /// <summary>
    /// Log4Net Implementation for ILogger
    /// </summary>
    public class Log4NetLogger : ILogger
    {
        /// <summary>
        /// Log4Net Logger
        /// </summary>
        private ILog logger;
 
        /// <summary>
        /// Default Ctor with XmlConfigurator
        /// </summary>
        public Log4NetLogger()
        {
            XmlConfigurator.Configure();
            this.logger = LogManager.GetLogger("Businessbingo");
        }

        /// <summary>
        /// Write Info-Log message to log
        /// </summary>
        /// <param name="context">Logging Context</param>
        /// <param name="messageFormat">Message Text</param>
        /// <param name="args">Arguments for Message</param>
        public void Info(string context, string messageFormat, params object[] args)
        {
            using (NDC.Push(context))
            {
                this.logger.InfoFormat(messageFormat, args);
            }
        }

        /// <summary>
        /// Write Debug-Log message to log
        /// </summary>
        /// <param name="context">Logging Context</param>
        /// <param name="messageFormat">Message Text</param>
        /// <param name="args">Arguments for Message</param>
        public void Debug(string context, string messageFormat, params object[] args)
        {
            using (NDC.Push(context))
            {
                this.logger.DebugFormat(messageFormat, args);
            }
        }

        /// <summary>
        /// Write Error-Log message to log
        /// </summary>
        /// <param name="context">Logging Context</param>
        /// <param name="messageFormat">Message Text</param>
        /// <param name="ex">Exception for logging</param>
        /// <param name="args">Arguments for Message</param>
        public void Error(string context, string messageFormat, Exception ex, params object[] args)
        {
            using (NDC.Push(context))
            {
                this.logger.ErrorFormat(messageFormat, args);
            }
        }
    }
}
